$(document).ready(function(){
	if (document.getElementById("tabs")) {
		$( "#tabs" ).tabs({
			show: function(event, ui) { 
				if(typeof eval(parent.restIframe)!=='undefined') {
					parent.restIframe()
					setTimeout("parent.restIframe()",500);
				}
			}
		});
	}
	if (document.getElementById("sortable")) $( "#sortable" ).sortable();
	
	//日期
	$( ".datepicker" ).datepicker({ dateFormat: 'yy-mm-dd'});
	$( ".datepicker" ).datepicker( $.datepicker.regional[ "zh-CN" ] );

	
	$("input[type=text]").not(".size").addClass("textinput");
	
	$(".articleSet").click(function(){
		var field = $(this).attr("field");
		var value = $(this).attr("value");
		var aid = $(this).attr("aid");
		overBox('<span style=""><img src="style/images/loading.gif"></span>');
		$.post('index.php?m=article&a=set',{'field':field,'value':value,'aid':aid},function(data){
			if (data == '1'){
				overBox('<span style="color:green;">修改成功</span>');
				setTimeout(function(){
					window.location.reload() 
				}, 500);
				//var articleSet = $(".articleSet[aid="+aid+"][value="+value+"][field="+field+"]");
				//if (articleSet.css("color")=='green')articleSet.css("color","#CCC")
				//	else articleSet.css("color","green");
			}else {
				overBox('<span style="color:red;">修改失败,请联系管理员</span>');
			}
		});
		return false;
	});
	$(".classifiedSet").click(function(){
		var field = $(this).attr("field");
		var value = $(this).attr("value");
		var id = $(this).attr("id");
		overBox('<span style=""><img src="style/images/loading.gif"></span>');
		$.post('index.php?m=classified&a=set',{'field':field,'value':value,'id':id,'classid':classid,'wid':wid},function(data){
			if (data == 1){
				overBox('<span style="color:green;">修改成功</span>');
				setTimeout(function(){
					window.location.reload() 
				}, 500);
				//var articleSet = $(".articleSet[aid="+aid+"][value="+value+"][field="+field+"]");
				//if (articleSet.css("color")=='green')articleSet.css("color","#CCC")
				//	else articleSet.css("color","green");
			}else {
				overBox('<span style="color:red;">修改失败,请联系管理员</span>');
			}
		});
		return false;
	});
	
	//生成页面的时候弹出进度框
	/*$(".make").click(function(){
		var url = $(this).attr("href");
		$("body").append('<div id="makeBox"><div id="progressbar"></div><br/><iframe id="ifr" name="ifr" src="" height="100" frameborder="no" width="100%"></iframe></div>');
		$("#makeBox").dialog({
			show: "slide",hide: "slide",title: "更新页面",width:500,height:300,
			open:function(){$( "#progressbar" ).progressbar({value: 0});$("#ifr").attr("src",url);},
			buttons: {
				"关闭": function(){
					$("#makeBox").dialog("close");
					$("#makeBox").dialog("destroy");
					$("#makeBox").remove();
					$("#progressbar").progressbar("destroy");
					$("#progressbar").remove();
				}
			}
		});
		return false;
	});*/
	$(".ajaxBox").click(function(){
        if (!$(this).attr("warn"));
        var warn = $(this).attr("warn") ? $(this).attr("warn") : '确定?';
	    if(confirm(warn)){
			var url = $(this).attr("href");
			$.post(url,{"ajax":"1"},function(data){
				if (data == '1'){
					overBox('<span style="color:green;">成功</span>');
					setTimeout(function(){window.location.reload();}, 1000);
					
				}else {
					overBox('<span style="color:red;">失败,请联系管理员</span>');
				}
			});
		}
		return false;
    });
	
	$(".delete").click(function(){
        if (!$(this).attr("warn"));
        var warn = $(this).attr("warn") ? $(this).attr("warn") : '确定要删除记录吗?';
	    if(confirm(warn)){
			var url = $(this).attr("href");
			$.post(url,{"ajax":"1"},function(data){
				if (data == '1'){
					overBox('<span style="color:green;">删除成功</span>');
					setTimeout(function(){window.location.reload();}, 1000);
					
				}else {
					overBox('<span style="color:red;">删除失败,请联系管理员</span>');
				}
			});
		}
		return false;
    });
	
	//重命名
	$(".rename").bind("click",function(){
		var cid	= $(this).attr("cid");
		var name = $(this).attr("name");
		$("#renameInput").attr("cid",cid);
		$("#renameInput").val(name);
		
		$("#init_page_dialog").dialog({
			show: "slide",
			hide: "slide",
			buttons: {
				"确定": function()
				{
					var cid  = $("#renameInput").attr("cid");
					var name = $("#renameInput").val();
					$.post("index.php?m=classify&a=rename",{cid:cid,name:name},function(data){
						if(data == '1'){
							$("#name_"+cid).html(name);
							$("#rename_"+cid).attr("name",name);
							overBox('<span style="color:green;">修改成功</span>');
						} else {
							overBox('<span style="color:red;">修改失败,请联系管理员</span>');
						}
					});
					$(this).dialog("close");
				},
				"关闭": function() {
					$(this).dialog("close");
				}
			}
		});
		$("#init_page_dialog").dialog("open");
	});
	
	//全选
	if(document.getElementById("checkAll")){
		var checkAll = document.getElementById("checkAll");
		
		var checkbox = document.getElementsByTagName("input");
		
		checkAll.onclick = function(){
			var checked = this.checked ? true : false;
			for(var i=0;i<checkbox.length;i++){
				if(checkbox[i].getAttribute("type") == "checkbox"){
					checkbox[i].checked = checked;
				}
			}
		}
	}
	
	//批量删除文章
	$("#articleDelete").click(function(){
		var warn = $(this).attr("warn") ? $(this).attr("warn") : '确定要删除记录吗?';
	    if(!confirm(warn)) return false;
		var aids = getCheckBox();
		$.post('index.php?m=article&a=delete&ajax=1',{'aid':aids},function(data){
			if (data == '1'){
				overBox('<span style="color:green;">修改成功</span>');
				setTimeout(function(){window.location.reload();}, 1000);
			}else {
				overBox('<span style="color:red;">修改失败,请联系管理员</span>');
			}
		});
	});
	
	//批量审核文章
	$("#articleAudit").click(function(){
		var aids = getCheckBox();
		$.post('index.php?m=article&a=set',{'field':'html','value':1,'aid':aids},function(data){
			if (data == '1'){
				overBox('<span style="color:green;">修改成功</span>');
				setTimeout(function(){window.location.reload();}, 1000);
			}else {
				overBox('<span style="color:red;">修改失败,请联系管理员</span>');
			}
		});
	});
	//if ($(".button").size()>0)$(".button").button();
/*    
	//帮助
	
	
	//在目标div里面载入页面
	divLoad();


	$(".article").click(function(){
		var url = $(this).attr("href");
		var aid = $(this).attr("aid");
		$.get(url,function(data){
			$("#articleBox").html(data);
		});

		$("#articleBox").dialog({
			show: "",
			hide: "",
			title:'修改文章',
			width: $(window).width() * 0.95,
			height: $(window).height() * 0.9,
			//modal:true,
			buttons: {
				"预览": function()
				{
					$("#form1").attr("action","index.php?m=data&a=update&type=page&preview=1");
					$("#form1").attr("target","_blank");
					$("#form1").submit();
				},
				"确定": function()
				{
					var getUrl ="index.php?m=article&a=edit";
					var input = $("#form1 :input");
					var query = '';
					for(var i=0;i<input.length;i++){
						if ((input.eq(i).attr("type") == 'checkbox')){
							if (input.eq(i).is(":checked")){
								query += '&'+input.eq(i).attr("name")+'='+input.eq(i).val();
							}
						} else if(input.eq(i).attr("name") == 'body'){
							query += '&body='+CKEDITOR.instances.body.getData();
						} else {
							query += '&'+input.eq(i).attr("name")+'='+input.eq(i).val();
						}
					}
					query += '&ajax=1';
					$.post(getUrl,query,function(data){
						if(data == '1'){
							articleBoxRemove();
							$("#stat_"+aid).html('<span style="color:green;">已审核</span>');
							overBox('<span style="color:green;">修改成功</span>');
							setTimeout(function(){window.location.reload();}, 1000);
						} else {
							articleBoxRemove();
							overBox('<span style="color:red;">修改失败,请联系管理员</span>');
						}
					});
				},
				"关闭": function() {
					articleBoxRemove();
				}
			}
		});	
		return false;
	});

	
	
	$(".cbg").hover(
		function(){$(this).addClass("c_bg")},
		function(){$(this).removeClass("c_bg")}
	);
	$(".move").bind("click",function(){
		var cid	= $(this).attr("cid");
		var name = $(this).attr("name");
		$("#renameInput").attr("cid",cid);
		$("#renameInput").val(name);
		
		$("#moveDialog").dialog({
			show: "slide",
			hide: "slide",
			buttons: {
				"确定": function()
				{
					var cid  = $("#renameInput").attr("cid");
					var name = $("#renameInput").val();
					$.post("index.php?m=classify&a=rename",{cid:cid,name:name},function(data){
						if(data == '1'){
							window.location.reload();
						} else {
							alert(data);
						}
					});
					$(this).dialog("close");
				},
				"关闭": function() {
					$(this).dialog("close");
				}
			}
		});
		$("#moveDialog").dialog("open");
	});

	
*/
});

function getCheckBox(){
	var checkbox = document.getElementsByTagName("input");
	var values = new Array;
	var s = 0;
	for(var i=0;i<checkbox.length;i++){
		if(checkbox[i].getAttribute("type") == "checkbox"){
			if (checkbox[i].checked && checkbox[i].value!=='')
			{
				values[s] = checkbox[i].value;
				s++;
			}
		}
	}
	return values;
}

function divLoad(){
	$(".divLoad").click(function(){
		var target = $(this).attr("target");
		var href = $(this).attr("href");
		load(target,href);
		//$(target).load(href);
		return false;
	})
}
function load(target,href){
	showOverlay();
	$(target).html("");
	$(target).load(href,function(){hideOverlay()});
}

//遮罩层
function showOverlay(){
	var winTop = $(window).scrollTop();
	var winHeight = $(window).height();
	var alertHeight = $(".alert").height();
	var top = winTop + winHeight/2 - alertHeight/2;
	$(".alert").css("top",top).add(".popUp").fadeIn();
	$(".alert a").click(function(){
		$(".popUp,.alert").hide();
	});
	$(".popUp").click(function(){hideOverlay()});
}

   
   //scroll
/*$(window).scroll(function(){
   var winTop = $(window).scrollTop();
   var winHeight = $(window).height();
   var alertHeight = $(".alert").height();
   var top = winTop + winHeight/2 - alertHeight/2;
   $(".popUp").css("height", winTop+winHeight+100 );
   $(".alert").css("top",top);
}); */   

var hideOverlay = function(){
	$(".popUp,.alert").fadeOut();
}

function articleBoxRemove(){
	$("#articleBox").dialog("close");
	$("#articleBox").dialog("destroy");
	$("#articleBox").html("");
}

function BoxRemove(obj){
	$(obj).dialog("close");
	$(obj).dialog("destroy");
	$(obj).html("");
}

function overBox(text){
	$("body").append('<div id="effect" style="display:none;"></div>');
	$("#effect").dialog({show: "",hide: "",title:text,dialogClass:"alert",width: "auto",height: "40",draggable:false,resizable:false});
	$("#effect").removeAttr( "style" ).hide();
	setTimeout(function(){$("#effect").dialog("close");$("#effect").dialog("destroy");$("#effect").remove();}, 1000);
}

function changeBarVal(val){
	$("#progressbar").progressbar({value: val});
	ifr.window.scroll(0, 999999)
}

function checkAll(obj)
{
	var checkbox = document.getElementsByTagName("input");
	var checked = obj.checked ? true : false;
	for(var i=0;i<checkbox.length;i++){
		if(checkbox[i].getAttribute("type") == "checkbox"){
			checkbox[i].checked = checked;
		}
	}
}

(function($){
    $.fn.hoverDelay = function(options){
        var defaults = {
            hoverDuring: 200,
            outDuring: 500,
            hoverEvent: function(){
                $.noop();
            },
            outEvent: function(){
                $.noop();
            }
        };
        var sets = $.extend(defaults,options || {});
        var hoverTimer, outTimer;
        return $(this).each(function(){
            $(this).hover(function(){
                clearTimeout(outTimer);
                hoverTimer = setTimeout(sets.hoverEvent, sets.hoverDuring);
            },function(){
                clearTimeout(hoverTimer);
                outTimer = setTimeout(sets.outEvent, sets.outDuring);
            });
        });
    }
})(jQuery);
/*
$(document).ready(function(){
	
	$(".float_div").draggable({handle:"#handle"});
	
	$(".float_div").hover(function(){
		$(".float_content").show();
	},function(){
		$(".float_content").hide();
	});	
	//$(".float_div").mouseout(function(){$(this).hide()});
	$(".menu_li").click(function(){
		var tbname = $(this).attr("tbname");
		var list = $(this).attr("list");
		if((list) == '1'){
			if (!document.getElementById("list_"+tbname)){
				$(".float_content").append('<div id="list_'+tbname+'" class="mode_list"></div>');
				$("#list_"+tbname).load("index.php?m="+tbname+"&a=list");
			}
			$(".mode_list").hide();
			$("#list_"+tbname).show();
			$(".float_content").show();
			$(".float_div").fadeIn("slow")
		} else {
			//load('.body','index.php?m='+tbname+'&a=main');	
		}
	});
});


//$(".menu_li[tbname=article]").click();

*/
function scrollx(p){
	var d = document,
	dd = d.documentElement,
	db = d.body,
	w = window,
	o = d.getElementById(p.id),
	ie6 = /msie 6/i.test(navigator.userAgent),style,timer;
	
	if(o){
		o.style.cssText +=";position:"+(p.f&&!ie6?'fixed':'absolute')+";"+(p.l==undefined?'right:0;':'left:'+p.l+'px;')+(p.t!=undefined?'top:'+p.t+'px':'bottom:0');
		if(p.f&&ie6){
			o.style.cssText +=';left:expression(documentElement.scrollLeft + '+(p.l==undefined?dd.clientWidth-o.offsetWidth:p.l)+' + "px");top:expression(documentElement.scrollTop +'+(p.t==undefined?dd.clientHeight-o.offsetHeight:p.t)+'+ "px" );';
			dd.style.cssText +=';background-image: url(about:blank);background-attachment:fixed;';
		}else{
			if(!p.f){
				w.onresize = w.onscroll = function(){
					clearInterval(timer);
					timer = setInterval(function(){
						//双选择为了修复chrome 下xhtml解析时dd.scrollTop为 0
						var st = (dd.scrollTop||db.scrollTop),c;
						c = st - o.offsetTop + (p.t!=undefined?p.t:(w.innerHeight||dd.clientHeight)-o.offsetHeight);
						if(c!=0){
							o.style.top = o.offsetTop + Math.ceil(Math.abs(c)/10)*(c<0?-1:1) + 'px';
						}else{
							clearInterval(timer);
						}
					},10)
				}
			}
		}
	}
}
/*
id 你要滚动的内容的id
l 横坐标的位置 不写为紧贴右边
t 你要放在页面的那个位置默认是贴着底边 0是贴着顶边
f 1表示固定 不写或者0表示滚动
*/